import { effectInsight } from "../../../../api/dashboard";
import { originEffectOptions } from "../../../common/echartOption";

let effectStatus = false;
export default function useEffectInsightData(formState: any, effectVal: any) {
  const effectOptions = JSON.parse(JSON.stringify(originEffectOptions));
  return new Promise(resolve => {
    const getEffectInsightData = async () => {
      let startTime = formState.rangedate[0].format("YYYY-MM-DD");
      let endTime = formState.rangedate[1].format("YYYY-MM-DD");
      let data = {
        startTime,
        endTime,
        platformAppCodeStr: formState.applet,
        actIdStr: formState.activities,
        //hdyx_uv 访问人数
        //hdyx_join_uv 入会人数
        eventCodeStr: "hdyx_uv,hdyx_join_uv"
      };
      let res = await effectInsight(data);
      if (res?.code === 0) {
        console.log("效果洞察", res);
        //遍历赋值
        for (let key in res.data) {
          if (key === effectVal.value) {
            effectOptions.yAxis.data = res.data[key].act_name;
            effectOptions.series[0].data = res.data[key].act_data;
            //如果有数据，就展示图表
            if (res?.data[key]?.act_name?.length > 0) {
              effectStatus = true;
            } else {
              effectStatus = false;
            }
          } else {
            console.log(222);
          }
          resolve({
            effectOptions,
            effectStatus
          });
        }
      }
    };
    getEffectInsightData();
  });
}
